package com.elmcode.server.service.impl;

import com.elmcode.server.constant.StatusConst;
import com.elmcode.server.dto.CategoryDTO;
import com.elmcode.server.entity.FoodEntity;
import com.elmcode.server.exception.BusException;
import com.elmcode.server.exception.CategoryException;
import com.elmcode.server.exception.ParamsException;
import com.elmcode.server.service.CategoryService;
import com.elmcode.server.service.FoodService;
import com.elmcode.server.vo.shopDetailVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elmcode.common.utils.PageUtils;
import com.elmcode.common.utils.Query;

import com.elmcode.server.dao.CategoryDao;
import com.elmcode.server.entity.CategoryEntity;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    FoodService foodService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
        String name = (String) params.get("name");
        if(!StringUtils.isEmpty(name)){
            queryWrapper.eq("name",name);
        }

        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void saveCategory(CategoryDTO categoryDTO) {

        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<CategoryEntity>().eq("name",categoryDTO.getName());

        List<CategoryEntity> entities = this.baseMapper.selectList(queryWrapper);
        if(entities.size() > 0){
            throw new CategoryException();
        }

        CategoryEntity categoryEntity = new CategoryEntity();
        BeanUtils.copyProperties(categoryDTO,categoryEntity);

        categoryEntity.setStatus(StatusConst.ACTIVE_STATUS);
        categoryEntity.setCreateTime(new Date());
        categoryEntity.setUpdateTime(new Date());
        this.baseMapper.insert(categoryEntity);
    }

    @Override
    public void updateCategoryInfo(Long id, CategoryDTO categoryDTO) {
        CategoryEntity categoryEntity = new CategoryEntity();
        categoryEntity.setId(id);

        BeanUtils.copyProperties(categoryDTO,categoryEntity);

        this.baseMapper.updateById(categoryEntity);
    }

    @Override
    public void deleteById(Long id){
        //TODO 删除的时候，还需要删除当前分类关联的菜品

        List<shopDetailVo> foodEntities = foodService.selectByCategoryId(id);
        if(foodEntities.size() == 0){
            this.baseMapper.deleteById(id);
        }else {
            throw new BusException("当前分类下存在菜品，删除失败");
        }
    }

    @Override
    public void updateStatus(Long id, String status) throws ParamsException {
        if(status == null){
            throw new ParamsException("参数错误");
        }
        Integer updateStatus = -1;
        if(StatusConst.ACTIVE_STATUS_NAME.equals(status)){
            updateStatus = StatusConst.ACTIVE_STATUS;
        }
        if(StatusConst.INACTIVE_STATUS_NAME.equals(status)){
            updateStatus = StatusConst.INACTIVE_STATUS;
        }

        if(updateStatus != -1){
            CategoryEntity categoryEntity = new CategoryEntity();
            categoryEntity.setId(id);
            categoryEntity.setStatus(updateStatus);
            categoryEntity.setUpdateTime(new Date());
            this.baseMapper.updateById(categoryEntity);
        }else {
            throw new ParamsException("参数错误");
        }
    }

    @Override
    public List<CategoryEntity> selectAllCategory() {
        return this.baseMapper.selectAllCategory();
    }

}